var f = function(){
    this.x=10;
    this.y = 100;
    this.needResetLocation = false;
};
/**
 * 打开对话框
 * @param url
 * @param data 传递到url地址的附加参数,用json表示
 * @param title 对话框的标题
 * @param width 对话框的宽度,若不指定或为null,则默认为480px,若指定必须为数字
 * @param noTitle 布尔值,显示显示对话框内部的标题,默认显示
 * @param formId 若指定了formId,则在成功获取表单内容后,为表单增加验证,主页面需要引入validate js(html5Validate)
 */
f.opend = function(url,data,title,width,noTitle,formId,formValidateHandler,okHandler){
    var $d = $("#_f_dialog");
    var _title = title || "详细数据";
    var isAddTitle = true;
    if(typeof noTitle === "boolean" && noTitle){
        isAddTitle = false;
    }
    if($d.length == 0){
        //页面上还没有对话框dom元素,创建之
        var html = "<div id=\"_f_dialog\" class=\"dialog\" style=\"display:none;\">" +
            "<span class=\"dialogTitle\"><span>"+_title+"</span><a href=\"javascript:void(0);\" onclick=\"_close_d();\"></a></span>" +
            "<div class=\"d_loading\">正加加载数据...</div>";
        if(isAddTitle){
            html += "<div class='d_title'><span id='d_head_title'>"+_title+"</span>(<span class='required'>*</span>表示为必填字段)</div>";
        }
        html += "<div class='d_error'></div>"+
            "<div id=\"_d_content\" class=\"d_content\"></div>" +
            "</div>";
        $d = $(html);
        $d.appendTo($(document.body));
        //为对话框增加移动效果
        $d.find(".dialogTitle").mousedown(function (e) { //e鼠标事件
            var offset = $d.offset(); //DIV在页面的位置
            var x = e.pageX - offset.left; //获得鼠标指针离DIV元素左边界的距离
            var y = e.pageY - offset.top; //获得鼠标指针离DIV元素上边界的距离
            $(document).bind("mousemove", function (ev)//绑定鼠标的移动事件，因为光标在DIV元素外面也要有效果，所以要用doucment的事件，而不用DIV元素的事件
            {
                $d.stop(); //加上这个之后
                var _x = ev.pageX - x; //获得X轴方向移动的值
                var _y = ev.pageY - y; //获得Y轴方向移动的值
                $d.animate({ left: _x + "px", top: _y + "px" }, 10);
            });
        });
        $(document).mouseup(function () {
            $(this).unbind("mousemove");
        });
    }else{
        $d.find(".dialogTitle span").html(_title);
        if(isAddTitle){
            $d.find(".d_title").show();
            $d.find("#d_head_title").html(_title);
        }else{
            $d.find(".d_title").hide();
        }
        //重新显示loading指示
        $d.find(".d_loading").css("display","block");
    }
    if(this.needResetLocation){
        this.needResetLocation = false;
        var w = 480;
        if(typeof width === "number" && width>0){
            w = width;
        }
        $d.css("width",w+"px");
        var ww=$(document).width();
        var dw=$d.width();
        if(this.x == 0){
            //居中显示
            this.x = (ww-dw)/2;
        }else{
            this.x= this.x-dw/2 - 20;
            if (this.x<0) this.x=10;
            if ((this.x+dw)>ww) this.x=ww-dw-20;
        }
    }
    $d.css({"display":"block","left": this.x+"px","top": this.y+"px"});
    //至此,页面上的dom元素已经准备完成,根据url从服务器上获取数据,并装载到内容块中
    //隐藏d_error的内容
    $d.find(".d_error").html("").hide();
    Http.doGetHtml(url,data,function(html){
        $("#_d_content").html(html);
        $d.find(".d_loading").css("display","none");
        if(typeof formId === "string" && formId.length>0){
            if(typeof formValidateHandler === "function"){
                $("#"+formId).html5Validate(formValidateHandler);
            }else{
                $("#"+formId).html5Validate();
            }
        }
        if(typeof okHandler === "function"){
            okHandler();
        }
    });
};
f.getHtml = function(url,params,divId,formId){
    var $loading = $(".loading");
    this.setError("");
    $loading.show();
    //计算loading显示的正中间位置
    var height = $(document).height()/3;
    var w = $(document).width()/2 - 200;
    $loading.css({"top":height+"px","left":w+"px"});
    Http.doGetHtml(url,params,function(html){
        var div = $("#"+divId);
        div.empty();
        div.html(html);
        if(typeof formId === "string" && formId.length>0){
            $("#"+formId).html5Validate();
        }
        $loading.hide();
    });
};
f.getBoxHtml = function (url,params,divId){
    var $div = $("#"+divId);
    var $loading = $div.find(".box-loading");
    var $body = $div.find(".box-body");
    $loading.show();
    Http.doGetHtml(url,params,function(html){
        $body.empty();
        $body.html(html);
        $loading.hide();
    });
};
f.post = function(url,params){

};
f.close = function(){
    this.setError("");
    _close_d();
};
f.setError = function(errHtml){
    /*var $err = $("#_f_dialog .d_error");
    if(typeof errHtml === "string" && errHtml.length>0){
        $err.css("display","block");
    }else{
        $err.css("display","none");
    }
    $err.html(errHtml);*/
    if(errHtml.length > 0){
        alert(errHtml);
    }
};

function _close_d(){
    $("#_d_content").html("");
    $('#_f_dialog').hide();
}
f.resetXY = function(sender){
    var b = false;
    if(sender){
        var xy = $(sender).offset();
        if(xy){
            b = true;
            this.x = xy.left;
            this.y = xy.top;
        }
    }
    if(!b){
        this.x = 0;
    }
    this.needResetLocation = true;
};
f.loadSelectItems = function($select,jsonArray,firstOptionName,defaultValue){
    $select.empty();
    var item,opt;
    var select = $select.get(0);
    if(typeof firstOptionName === "string" && firstOptionName != ""){
        opt = new Option(firstOptionName,"");
        select.options.add(opt);
    }
    for (var i = 0; i < jsonArray.length; i++) {
        item = jsonArray[i];
        opt = new Option(item.name,item.id);
        select.options.add(opt);
    }
    if(typeof defaultValue === "string" && defaultValue != ""){
        $select.val(defaultValue);
    }
};